home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / parser.h < prev    next >
C/C++ Source or Header  |  2000-01-06  |  5KB  |  174 lines

  1. // $Id: parser.h,v 1.6 2000/01/06 06:46:47 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #ifndef parser_INCLUDED
  11. #define parser_INCLUDED
  12.  
  13. #include "config.h"
  14. #include <limits.h>
  15. #include <ctype.h>
  16. #ifdef HAVE_WCHAR_H
  17. # include <wchar.h>
  18. #endif
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include "lpginput.h"
  22.  
  23. class StoragePool;
  24. class AstPackageDeclaration;
  25. class AstCompilationUnit;
  26. class AstClassBody;
  27. class AstInterfaceDeclaration;
  28. class Ast;
  29. class AstListNode;
  30.  
  31. struct SecondaryRepairInfo
  32. {
  33.     int code,
  34.         distance,
  35.         buffer_position,
  36.         stack_position,
  37.         num_deletions,
  38.         symbol;
  39.  
  40.     bool recovery_on_next_stack;
  41. };
  42.  
  43. class Parser : public javaprs_table
  44. {
  45. public:
  46.  
  47.     Parser() : ast_pool(NULL),
  48.                parse_header_only(false),
  49.                parse_package_header_only(false),
  50.                location_stack(NULL),
  51.                parse_stack(NULL),
  52.                stack_length(0),
  53.                stack(NULL),
  54.                temp_stack(NULL)
  55.     {
  56.         InitRuleAction();
  57.         return;
  58.     }
  59.  
  60.     ~Parser()
  61.     {
  62.         delete [] stack;
  63.         delete [] location_stack;
  64.         delete [] parse_stack;
  65.         delete [] temp_stack;
  66.     }
  67.  
  68.     AstPackageDeclaration *PackageHeaderParse(LexStream *, StoragePool *);
  69.     AstCompilationUnit *HeaderParse(LexStream *, StoragePool * = NULL);
  70.     bool InitializerParse(LexStream *, AstClassBody *);
  71.     bool InitializerParse(LexStream *, AstInterfaceDeclaration *);
  72.     bool BodyParse(LexStream *, AstClassBody *);
  73.     bool BodyParse(LexStream *, AstInterfaceDeclaration *);
  74.  
  75. protected:
  76.  
  77.     TokenObject buffer[BUFF_SIZE];
  78.     TokenObject end_token;
  79.  
  80.     Ast *HeaderParse();
  81.     bool Initializer(AstClassBody *);
  82.     bool Initializer(AstInterfaceDeclaration *);
  83.     bool Body(AstClassBody *);
  84.     bool Body(AstInterfaceDeclaration *);
  85.     Ast *ParseSegment(TokenObject);
  86.  
  87. #define HEADERS
  88. #include "javaact.h"
  89.  
  90.     void (Parser::*rule_action[NUM_RULES + 1]) ();
  91.  
  92.     void InitRuleAction();
  93.  
  94.     //******************************************************************************
  95.     //
  96.     // Given a rule of the form     A ::= x1 x2 ... xn     n > 0
  97.     //
  98.     // the function Token(i) yields the symbol xi, if xi is a terminal
  99.     // or ti, if xi is a nonterminal that produced a string of the form
  100.     // xi => ti w.
  101.     //
  102.     //******************************************************************************
  103.     inline LexStream::TokenIndex Token(int i)
  104.     {
  105.         return location_stack[state_stack_top + (i - 1)];
  106.     }
  107.  
  108.     //******************************************************************************
  109.     //
  110.     // Given a rule of the form     A ::= x1 x2 ... xn     n > 0
  111.     //
  112.     // the function Sym(i) yields the AST subtree associated with symbol
  113.     // xi. NOTE that if xi is a terminal, Sym(i) is undefined !
  114.     //
  115.     //******************************************************************************
  116.     inline Ast*& Sym(int i) { return parse_stack[state_stack_top + (i - 1)]; }
  117.  
  118.     //******************************************************************************
  119.     //
  120.     // When a token is shifted, we also construct a null AST for
  121.     // it.  This is necessary in case we encounter an error and need to
  122.     // delete AST subtrees from the parse stack - those corresponding to
  123.     // shifted tokens should also have a valid subtree.
  124.     //
  125.     //******************************************************************************
  126.     inline void TokenAction(TokenObject curtok) { Sym(1) = NULL; }
  127.  
  128.     LexStream *lex_stream;
  129.  
  130.     StoragePool *ast_pool,
  131.                 *body_pool,
  132.                 *list_node_pool;
  133.  
  134.     AstListNode *free_list_nodes;
  135.     AstListNode *AllocateListNode();
  136.     void FreeCircularList(AstListNode *);
  137.  
  138.     bool parse_header_only,
  139.          parse_package_header_only;
  140.  
  141.     //
  142.     // LOCATION_STACK is a stack that is "parallel" to
  143.     // (STATE_)STACK that is used to keep
  144.     // track of the location of the first token on which an action
  145.     // was executed in the corresponding state.
  146.     //
  147.     Location *location_stack;
  148.     Ast **parse_stack;
  149.  
  150.     enum { STACK_INCREMENT = 256 };
  151.  
  152.     int stack_length,
  153.         state_stack_top,
  154.         *stack,
  155.  
  156.         temp_stack_top,
  157.         *temp_stack;
  158.  
  159.     static inline int Min(int x, int y) { return ((x) < (y) ? (x) : (y)); }
  160.     static inline int Max(int x, int y) { return ((x) > (y) ? (x) : (y)); }
  161.  
  162.     void AllocateErrorStacks();
  163.     void ReallocateStacks();
  164.     void ErrorRepair(TokenObject error_token);
  165.     void RepairParse(TokenObject);
  166.     SecondaryRepairInfo ErrorSurgery(int stack[],
  167.                                      int stack_top,
  168.                                      int last_index,
  169.                                      SecondaryRepairInfo repair);
  170.     int ParseCheck(int stack[], int stack_top, int first_token, int buffer_position);
  171. };
  172.  
  173. #endif
  174.